package com.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.ssm.base.BaseController;
import com.ssm.entity.*;
import com.ssm.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author jie_ge
 * @Date 2022/4/18
 */
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {
    @Reference
    private HouseService houseService;
    @Reference
    private DictService dictService;
    @Reference
    private CommunityService communityService;
    @Reference
    private HouseImageService houseImageService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private HouseUserService houseUserService;
    /**
     * 去首页  查询分页及其二级查询
     * @return
     */
    @RequestMapping
    public String index(Map map, HttpServletRequest request){
        Map<String, Object> filters = getFilters(request);
        //获取分页数据
        PageInfo<House> page = houseService.findPage(filters);
        map.put("page",page);
        map.put("filters",filters);
        //查询所有的小区信息
        map.put("communityList", communityService.findAll());
        //查询房屋类型列表
        map.put("houseTypeList",dictService.findListByDictCode("houseType"));
        //查询楼层类型列表
        map.put("floorList",dictService.findListByDictCode("floor"));
        //查询建筑状态列表
        map.put("buildStructureList",dictService.findListByDictCode("buildStructure"));
        //查询房屋朝向列表
        map.put("directionList",dictService.findListByDictCode("direction"));
        //查询房屋装饰列表
        map.put("decorationList",dictService.findListByDictCode("decoration"));
        //查询房屋使用者
        map.put("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/index";
    }

    /**
     * 跳转到添加页面
     * @param map
     * @return
     */
    @GetMapping("/create")
    public String create(Map map) {

        //查询所有的小区信息
        map.put("communityList", communityService.findAll());
        //查询房屋类型列表
        map.put("houseTypeList",dictService.findListByDictCode("houseType"));
        //查询楼层类型列表
        map.put("floorList",dictService.findListByDictCode("floor"));
        //查询建筑状态列表
        map.put("buildStructureList",dictService.findListByDictCode("buildStructure"));
        //查询房屋朝向列表
        map.put("directionList",dictService.findListByDictCode("direction"));
        //查询房屋装饰列表
        map.put("decorationList",dictService.findListByDictCode("decoration"));
        //查询房屋使用者
        map.put("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/create";
    }

    /**
     * 保存
     * @param house
     * @return
     */
    @PostMapping("/save")
    public String save(House house) {
        //添加到数据库
        houseService.insert(house);
        return "common/successPage";
    }

    /**
     * 修改
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/edit/{id}")
    public String toString(@PathVariable("id") Long id,Map map) {
        House house = houseService.getById(id);
        map.put("house",house);
        //查询所有的小区信息
        map.put("communityList", communityService.findAll());
        //查询房屋类型列表
        map.put("houseTypeList",dictService.findListByDictCode("houseType"));
        //查询楼层类型列表
        map.put("floorList",dictService.findListByDictCode("floor"));
        //查询建筑状态列表
        map.put("buildStructureList",dictService.findListByDictCode("buildStructure"));
        //查询房屋朝向列表
        map.put("directionList",dictService.findListByDictCode("direction"));
        //查询房屋装饰列表
        map.put("decorationList",dictService.findListByDictCode("decoration"));
        //查询房屋使用者
        map.put("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/edit";
    }

    /**
     * 保存修改
     * @param house
     * @return
     */
    @PostMapping("/update")
    public String update(House house) {
        houseService.update(house);
        return "common/successPage";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public String toString(@PathVariable("id") Long id) {
        houseService.deleteById(id);
        return "redirect:/house";
    }

    /**
     * 房屋发布状态
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/publish/{id}/{status}")
    public String publish(@PathVariable("id") Long id,@PathVariable("status") Integer status) {
        houseService.publish(id,status);
        return "redirect:/house";
    }

    /**
     * 展示图片
     * @param map
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public String houseShow(Map map,@PathVariable("id") Long id) {
        House house = houseService.getById(id);
        Community community = communityService.getById(house.getCommunityId());
        //经纪人
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        //租户
        List<HouseUser> houseUserList = houseUserService.findListByHouseId(id);
        //房屋图片
        List<HouseImage> houseImage1List = houseImageService.findList(id, 1);
        //房产图片
        List<HouseImage> houseImage2List = houseImageService.findList(id, 2);
        map.put("house",house);
        map.put("community",community);
        map.put("houseUserList",houseUserList);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseImage1List",houseImage1List);
        map.put("houseImage2List",houseImage2List);
        return "house/show";
    }
}
